package drds.server.net.packet;

import java.nio.ByteBuffer;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class ByteBufferUtil {

	public static final void writeInt2(ByteBuffer byteBuffer, int $int) {
		byteBuffer.put((byte) ($int & 0xff));
		byteBuffer.put((byte) ($int >>> 8));
	}

	public static final void writeInt3(ByteBuffer byteBuffer, int $int) {
		byteBuffer.put((byte) ($int & 0xff));
		byteBuffer.put((byte) ($int >>> 8));
		byteBuffer.put((byte) ($int >>> 16));
	}

	public static final void writeInt4(ByteBuffer byteBuffer, int $int) {
		byteBuffer.put((byte) ($int & 0xff));
		byteBuffer.put((byte) ($int >>> 8));
		byteBuffer.put((byte) ($int >>> 16));
		byteBuffer.put((byte) ($int >>> 24));
	}

	public static final void writeLong4(ByteBuffer byteBuffer, long $long) {
		byteBuffer.put((byte) ($long & 0xff));
		byteBuffer.put((byte) ($long >>> 8));
		byteBuffer.put((byte) ($long >>> 16));
		byteBuffer.put((byte) ($long >>> 24));
	}

	public static final void writeLong8(ByteBuffer byteBuffer, long $long) {
		byteBuffer.put((byte) ($long & 0xff));
		byteBuffer.put((byte) ($long >>> 8));
		byteBuffer.put((byte) ($long >>> 16));
		byteBuffer.put((byte) ($long >>> 24));
		byteBuffer.put((byte) ($long >>> 32));
		byteBuffer.put((byte) ($long >>> 40));
		byteBuffer.put((byte) ($long >>> 48));
		byteBuffer.put((byte) ($long >>> 56));
	}

	public static final void writeLength(ByteBuffer byteBuffer, long $long) {
		if ($long < 251) {
			byteBuffer.put((byte) $long);
		} else if ($long < 0x10000L) {
			byteBuffer.put((byte) 252);
			writeInt2(byteBuffer, (int) $long);
		} else if ($long < 0x1000000L) {
			byteBuffer.put((byte) 253);
			writeInt3(byteBuffer, (int) $long);
		} else {
			byteBuffer.put((byte) 254);
			writeLong8(byteBuffer, $long);
		}
	}

	public static final void writeWithNull(ByteBuffer byteBuffer, byte[] bytes) {
		byteBuffer.put(bytes);
		byteBuffer.put((byte) 0);
	}

	public static final void writeWithLength(ByteBuffer byteBuffer, byte[] bytes) {
		int length = bytes.length;
		if (length < 251) {
			byteBuffer.put((byte) length);
		} else if (length < 0x10000L) {
			byteBuffer.put((byte) 252);
			writeInt2(byteBuffer, length);
		} else if (length < 0x1000000L) {
			byteBuffer.put((byte) 253);
			writeInt3(byteBuffer, length);
		} else {
			byteBuffer.put((byte) 254);
			writeLong8(byteBuffer, length);
		}
		byteBuffer.put(bytes);
	}

	public static final void writeWithLength(ByteBuffer byteBuffer, byte[] bytes, byte nullValue) {
		if (bytes == null) {
			byteBuffer.put(nullValue);
		} else {
			writeWithLength(byteBuffer, bytes);
		}
	}

	public static final int getLength(long length) {
		if (length < 251) {
			return 1;
		} else if (length < 0x10000L) {
			return 3;
		} else if (length < 0x1000000L) {
			return 4;
		} else {
			return 9;
		}
	}

	public static final int getLength(byte[] bytes) {
		int length = bytes.length;
		if (length < 251) {
			return 1 + length;
		} else if (length < 0x10000L) {
			return 3 + length;
		} else if (length < 0x1000000L) {
			return 4 + length;
		} else {
			return 9 + length;
		}
	}

}